<?php
$id=$_GET['id']?$_GET['id']:NULL;
$product_id=$_GET['product_id']?$_GET['product_id']:NULL;
$key=$_GET['key']?$_GET['key']:NULL;
$catid=$_GET['catid']?$_GET['catid']:NULL;
$code=$_GET['code']?$_GET['code']:NULL;
$tags=$_GET['tags']?$_GET['tags']:NULL;

if(!empty($key))
	$scl.=" and ( pname like '%$key%' )";
if(!empty($catid))
	$scl.=" and LOCATE($catid,catid)=1";
if(!empty($tags))
	$scl.=" and (tags like '$tags%' or tags like '%,$tags%') ";
if(!empty($code))
	$scl.=" and code = '$code'";
if(!empty($id))
	$scl.=" and id <> '$id'";
if(!empty($product_id))
	$scl.=" and id in($product_id)";

if($_GET['brand'] && $_GET['brandid']){
	$scl.=" and brandid = '$_GET[brandid]'";
}else{
	$_GET['brand']=addslashes($_GET['brand']);
	$scl.=" and brand like '$_GET[brand]%'";
}

$sql="SELECT id,catid,tags,brand,pic,pname,price,show_price,market_price,code,tax_rate FROM ".PRO." where status>0 $scl order by id desc";
//====================
include_once("../includes/page_utf_class.php");
$page = new Page;
$page->listRows=10;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$page->totalRows = $db->num_rows();
}
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=====================
$db->query($sql);
$de['list']=$db->getRows();
//$tpl->assign("de",$de);

if(!empty($catid) and $de['list'])
{
	$max=$catid.'99';
	$min=$catid.'00';
	$ss=" and catid<=$max and catid>=$min ";
}
elseif(!empty($catid) and !$de['list'])
{
	$catid=substr($catid,0,-2);
	$max=$catid.'99';
	$min=$catid.'00';
	$ss=" and catid<=$max and catid>=$min ";
}
else
	$ss=" and catid<9999 ";

$sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
$db->query($sql);
$re=$db->getRows();
if(!$re)
{
	$catid=substr($catid,0,-2);
	$max=$catid.'99';
	$min=$catid.'00';
	$ss=" and catid<=$max and catid>=$min ";
	$sql="SELECT cat,catid FROM ".PCAT." where 1 $ss order by catid";
	$db->query($sql);
	$re=$db->getRows();
}
$tpl->assign("re",$re);

include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
//================================================================
$pro=new pro();
$de['all_tags']=$pro->get_tags();

if ($_GET['tags']){

	$de['tags']=array($_GET['tags']);


}

//所属分类，产品标签
$catids = "";
$tagids = "";
$catTmp = array();
$tagTmp = array();
foreach ($de['list'] as $dk=>$dv)
{
	$catids .= $dv['catid'].",";
	$tagids .= $dv['tags'].",";
}
$catids = rtrim($catids,",");
if($catids!=""){
	$sql="SELECT cat,catid FROM ".PCAT." WHERE catid IN (".$catids.")";
	$db->query($sql);
	$catTmp=$db->getRows();
}
$catArr = array();
foreach ($catTmp as $cav){
	$catArr[$cav['catid']] = $cav['cat'];
}
$tagids = rtrim($tagids,",");
if($tagids!=""){
	$sql="SELECT id,tags FROM ".TAG." WHERE id IN (".$tagids.")";
	$db->query($sql);
	$tagTmp=$db->getRows();
}
$tagArr = array();
foreach ($tagTmp as $tav)
{
	$tagArr[$tav['id']] = $tav['tags'];
}


foreach ($de['list'] as $dk=>$dv)
{
	$de['list'][$dk]['catname'] = $catArr[$dv['catid']];
	$tagsTmp = explode(",", $dv['tags']);
	$tagStr = "";
	foreach ($tagsTmp as $ttv)
	{
		$tagStr .= $tagArr[$ttv].",";
	}
	$de['list'][$dk]['tagnames'] = rtrim($tagStr,",");
}

$tpl->assign("de",$de);
		
	$tpl->assign("config",$config);
	$tpl->display("product_list.htm");
	
?>